using Digitalmes.Domain.Entities.Mes.Equipments;

namespace Digitalmes.Application.Mes.Equipments.Inspections.Queries;

/// <summary>
/// 设备点检计划分页查询命令。
/// </summary>
public sealed record GetInspectionPlanListWithPagedQuery(
    long? LineId,
    string? MachineryName,
    int PageNumber,
    int PageSize) : IPagedQuery, IRequest<IResult<PagedList<EquipInspectionPlanDto>>>;

internal sealed class GetInspectionPlanListWithPagedQueryHandler(IRepository<EquipInspectionPlan> repo)
    : IRequestHandler<GetInspectionPlanListWithPagedQuery, IResult<PagedList<EquipInspectionPlanDto>>>
{
    public async Task<IResult<PagedList<EquipInspectionPlanDto>>> Handle(GetInspectionPlanListWithPagedQuery request, CancellationToken cancellationToken)
    {
        var items = await repo.AsQueryable()
            .Includes(m => m.Machinery, x => x!.Line)
            .Includes(m => m.Machinery, x => x!.Workstation)
            .WhereIF(request.LineId > 0, m => m.Machinery!.LineId == request.LineId)
            .WhereIF(!string.IsNullOrEmpty(request.MachineryName), m => m.Machinery!.MachineryName.Contains(request.MachineryName!))
            .GetPagedListAsync(request.PageNumber, request.PageSize, cancellationToken);
        return Result.From(items.Adapt<PagedList<EquipInspectionPlanDto>>());
    }
}
